const { Sequelize, DataTypes, Model, QueryTypes, Deferrable, Op } = require('sequelize')
const sequelize = new Sequelize('nodeExpress04', 'root', '123456', {
    host: 'localhost',
    dialect: 'mysql',
    define: {
        freezeTableName: true
    }
})
const Class = sequelize.define('class', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        unique: true
    }
})
const User = sequelize.define('user', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    avatar: {
        type: DataTypes.STRING,
        defaultValue: 'http://localhost:3000/image/avatar.png'
    },
    nickname: {
        type: DataTypes.STRING,
        allowNull: false
    },
    username: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false
    },
    type: {
        type: DataTypes.ENUM('admin', 'teacher', 'student'),
        allowNull: false,
        defaultValue: 'student'
    },
    class_id: {
        type: DataTypes.INTEGER
    }
})
const Subject = sequelize.define('subject', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        unique: true
    }
})
const Course = sequelize.define('course', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        unique: true
    },
    video: {
        type: DataTypes.STRING
    },
    img: {
        type: DataTypes.STRING
    },
    description: {
        type: DataTypes.STRING
    },
    filePath: {
        type: DataTypes.STRING
    },
    subject_id: {
        type: DataTypes.INTEGER,
        references: {
            model: Subject,
            key: 'id',
            deferrable: Deferrable.INITIALLY_IMMEDIATE
        }
    },
    class_id: {
        type: DataTypes.INTEGER,
        references: {
            model: Class,
            key: 'id',
            deferrable: Deferrable.INITIALLY_IMMEDIATE
        }
    },
    teacher_id: {
        type: DataTypes.INTEGER,
        references: {
            model: User,
            key: 'id',
            deferrable: Deferrable.INITIALLY_IMMEDIATE
        }
    }
})
const Homework = sequelize.define('homework', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    title: {
        type: DataTypes.STRING
    },
    img: {
        type: DataTypes.STRING
    },
    answer: {
        type: DataTypes.STRING
    },
    startTime: {
        type: DataTypes.DATE
    },
    endTime: {
        type: DataTypes.DATE
    },
    course_id: {
        type: DataTypes.INTEGER,
        references: {
            model: Course,
            key: 'id',
            deferrable: Deferrable.INITIALLY_IMMEDIATE
        }
    },
    isProgram: {
        type: DataTypes.ENUM('true', 'false'),
        allowNull: false,
        defaultValue: 'false'
    },
    file: {
        type: DataTypes.STRING
    }
})
const HomeworkLog = sequelize.define('homeworkLog', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    student_id: {
        type: DataTypes.INTEGER,
        references: {
            model: User,
            key: 'id',
            deferrable: Deferrable.INITIALLY_IMMEDIATE
        }
    },
    homework_id: {
        type: DataTypes.INTEGER,
        references: {
            model: Homework,
            key: 'id',
            deferrable: Deferrable.INITIALLY_IMMEDIATE
        }
    },
    studentAnswer: {
        type: DataTypes.STRING
    },
    score: {
        type: DataTypes.INTEGER
    },
    teacherRemark: {
        type: DataTypes.STRING
    },
    file: {
        type: DataTypes.STRING
    }
})

const foo = async () => {
    await sequelize.sync({})
}
foo()
module.exports = {
    User,
    Subject,
    Class,
    Course,
    QueryTypes,
    sequelize,
    Op,
    Homework,
    HomeworkLog,
}
/*
* 这里面的表必须自己去数据库建，这里只是拿到表的信息
* 不要小看这个文件，它新增的时候，将新增哪些数据，是由这里面决定的，查询等也是，它就认为数据库中表的字段只有这些
* */
